<template>
  <el-card>
    <el-table
      :data="skuList"
      border
      :row-style="{ height: '84px' }"
      v-loading="loading"
    >
      <el-table-column type="index" label="序号" width="100" />
      <el-table-column property="skuName" label="名称" />
      <el-table-column property="skuDesc" label="描述" />
      <el-table-column label="默认图片">
        <template v-slot="{ row }">
          <img :src="row.skuDefaultImg" class="spu-img" />
        </template>
      </el-table-column>
      <el-table-column property="price" label="价格" />
      <el-table-column property="weight" label="重量" />
      <el-table-column label="操作" align="center" width="300">
        <template v-slot="{ row }">
          <el-tooltip
            :content="`${row.isSale == 1 ? '下架' : '上架'}`"
            placement="bottom-end"
            effect="light"
            v-if="row.isSale !== 1"
          >
            <el-button
              size="small"
              type="primary"
              :icon="Top"
              @click="ChangeIsSale(row)"
            />
          </el-tooltip>
          <el-tooltip
            :content="`${row.isSale == 1 ? '下架' : '上架'}`"
            placement="bottom-end"
            effect="light"
            v-else
          >
            <el-button
              size="small"
              type="success"
              :icon="Bottom"
              @click="ChangeIsSale(row)"
            />
          </el-tooltip>
          <el-tooltip content="修改SkU" placement="bottom-end" effect="light">
            <el-button size="small" type="primary" :icon="Edit" />
          </el-tooltip>
          <el-tooltip content="查看S详情" placement="bottom-end" effect="light">
            <el-button size="small" type="info" :icon="InfoFilled" />
          </el-tooltip>

          <el-tooltip content="删除SKU" placement="bottom-end" effect="light">
            <div class="spu-del">
              <el-popconfirm
                :title="`您确定要删除${row.skuName}吗?`"
                @confirm="delSku(row)"
              >
                <template #reference>
                  <el-button
                    type="danger"
                    size="small"
                    :icon="Delete"
                  ></el-button>
                </template>
              </el-popconfirm>
            </div>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页器 -->
    <el-pagination
      class="spu"
      v-model:currentPage="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[10, 20, 30]"
      layout="prev, pager, next, jumper,->,sizes,total"
      :total="total"
      @size-change="getSkuList"
      @current-change="getSkuList"
    />
  </el-card>
</template>

<script lang="ts">
export default {
  name: "ProductSku",
};
</script>

<script lang="ts" setup>
import { ref, onMounted } from "vue";
import { ElMessage } from "element-plus";

import { Bottom, Edit, Delete, InfoFilled, Top } from "@element-plus/icons-vue";
import {
  getSkuListPageApi,
  delSkuListPageApi,
  onSaleApi,
  cancelApi,
} from "@/api/product/sku";

const currentPage = ref(1);
const pageSize = ref(10);
const total = ref(0);
const loading = ref(false);

//获取sku列表
const skuList = ref([]);

//封装请求函数
const getSkuList = async () => {
  loading.value = true;
  const res = await getSkuListPageApi(currentPage.value, pageSize.value);
  loading.value = false;
  skuList.value = res.records;
  total.value = res.total;
};

onMounted(getSkuList);

//删除sku
const delSku = async (row) => {
  await delSkuListPageApi(row.id);
  ElMessage.success("删除SKU成功");
  getSkuList();
};

//上架下架
const ChangeIsSale = async (row) => {
  if (row.isSale == 1) {
    await cancelApi(row.id);
    ElMessage.success("下架SKU成功");
    row.isSale = 0;
  } else {
    await onSaleApi(row.id);
    ElMessage.success("上架SKU成功");
    row.isSale = 1;
  }
};
</script>

<style scoped>
.spu-del {
  display: inline-block;
  margin-left: 10px;
}
.spu {
  margin-top: 20px;
}
.spu-img {
  width: 100px;
}
</style>
